import matplotlib.pyplot as plt
import numpy as np

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

# 摆线参数
r = 1  # 圆的半径
t = np.linspace(0, 4*np.pi, 1000)  # 参数t从0到4π，生成1000个点

# 摆线参数方程
x = r * (t - np.sin(t))
y = r * (1 - np.cos(t))

# 创建图形
fig, ax = plt.subplots(figsize=(14, 8))

# 绘制摆线轨迹
ax.plot(x, y, 'b-', linewidth=2, label='摆线轨迹')

# 绘制基线（x轴）
ax.axhline(y=0, color='k', linewidth=1.5, alpha=0.7, label='基线')

# 绘制母圆在几个关键时刻的位置
key_times = [0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi, 5*np.pi/2, 3*np.pi]
colors = plt.cm.viridis(np.linspace(0, 1, len(key_times)))  # 使用颜色映射

for i, t_val in enumerate(key_times):
    # 圆心位置：圆心沿x轴移动距离为r*t，y坐标为r
    center_x = r * t_val
    center_y = r
    
    # 绘制母圆
    circle_theta = np.linspace(0, 2*np.pi, 100)
    circle_x = center_x + r * np.cos(circle_theta)
    circle_y = center_y + r * np.sin(circle_theta)
    ax.plot(circle_x, circle_y, '--', color=colors[i], alpha=0.6, linewidth=1)
    
    # 标记圆心
    ax.plot(center_x, center_y, 'o', color=colors[i], markersize=4)
    
    # 计算摆点位置
    point_x = r * (t_val - np.sin(t_val))
    point_y = r * (1 - np.cos(t_val))
    ax.plot(point_x, point_y, 'o', color=colors[i], markersize=6)
    
    # 连接圆心和摆点（半径）
    ax.plot([center_x, point_x], [center_y, point_y], ':', color=colors[i], alpha=0.7, linewidth=1)
    
    # 添加角度标注
    ax.text(center_x, center_y + r + 0.1, f't={t_val/np.pi:.1f}π', 
            fontsize=8, ha='center', color=colors[i])

# 标记关键点
# 起点
ax.plot(0, 0, 'mo', markersize=8, label='起点')
ax.text(0.2, 0.2, '起点(0,0)', fontsize=10)

# 第一个拱的最高点
t_max = np.pi
x_max = r * (t_max - np.sin(t_max))
y_max = r * (1 - np.cos(t_max))
ax.plot(x_max, y_max, 'ro', markersize=8, label='拱顶点')
ax.text(x_max - 0.5, y_max + 0.2, f'拱顶点({x_max:.1f},{y_max:.1f})', fontsize=10)

# 第一个拱的终点
t_end = 2*np.pi
x_end = r * (t_end - np.sin(t_end))
y_end = r * (1 - np.cos(t_end))
ax.plot(x_end, y_end, 'go', markersize=8, label='拱终点')
ax.text(x_end - 0.5, y_end + 0.2, f'拱终点({x_end:.1f},{y_end:.1f})', fontsize=10)

# 设置图形属性
ax.set_xlim(-0.5, 4*np.pi*r + 0.5)
ax.set_ylim(-0.2, 2.2*r)
ax.set_aspect('equal')  # 保证比例相等
ax.grid(True, alpha=0.3)
ax.set_xlabel('x', fontsize=12)
ax.set_ylabel('y', fontsize=12)
ax.set_title('摆线的形成过程', fontsize=14, pad=20)

# 添加图例
ax.legend(loc='upper right', fontsize=10)

# 添加参数方程说明
formula_text = f'摆线参数方程:\n'
formula_text += f'$x = r(t - \\sin t)$\n'
formula_text += f'$y = r(1 - \\cos t)$\n'
formula_text += f'其中 $r = {r}$'
ax.text(0.02, 0.98, formula_text, transform=ax.transAxes, fontsize=11,
        verticalalignment='top', bbox=dict(boxstyle="round", facecolor='wheat', alpha=0.8))

# 添加摆线性质说明
properties_text = "摆线性质:\n"
properties_text += "• 一个拱的宽度: $2\\pi r$\n"
properties_text += "• 拱高: $2r$\n"
properties_text += "• 一个拱的弧长: $8r$"
ax.text(0.75, 0.98, properties_text, transform=ax.transAxes, fontsize=10,
        verticalalignment='top', bbox=dict(boxstyle="round", facecolor='lightblue', alpha=0.8))

plt.tight_layout()
plt.show()